package demo2.observerImpl;

import demo2.DisplayElement;
import demo2.Subject;
import demo2.subjectImpl.WeatherData;

import java.util.Observable;
import java.util.Observer;

/**
 * @author 24岁没牵过女人的手 日期：2023/6/29 15:47 文件描述：
 */
public class CurrentConditionDisplay implements Observer, DisplayElement {
  private double temperature;
  private double humidity;
  private Observable observable;

  public CurrentConditionDisplay(Observable observable) {
    this.observable = observable;
    observable.addObserver(this);
  }

  @Override
  public void display() {
    System.out.println("CurrentCondition: " + temperature + "℃" + humidity * 100 + "% humidity");
  }


  @Override
  public void update(Observable o, Object arg) {
      if(o instanceof WeatherData){
        WeatherData weatherData = (WeatherData) o;
        this.temperature = weatherData.getTemperature();
        this.humidity  = weatherData.getHumidity();
        display();
      }
  }
}
